<?php
namespace app\agent\controller\cash;

use app\agent\controller\Controller;
use app\agent\model\wxapp\Wxapp as WxappModel;
use think\Db;
use think\Session;

class Agentcash extends Controller{
    /**
     * 代理提现首页
     */
    public function indexagent(){
        $session = Session::get('yoshop_agent');
        $agent_id = $session['agent']['agent_id'];
        $model = new WxappModel;
        //var_dump($session);die;
        $res=Db::name('agent')->where(['pinpai_id'=>$session['agent']['pinpai_id']])->select();
        $return=$this->getTree($res,$agent_id,'true');
        $str=implode(',',$return);
        //var_dump($str);
        $page=isset($pagenum)&&is_numeric(input('post.page_num'))?intval($pagenum):1;
        $limit = 10;
        $offset=($page-1) * $limit;
        return $this->fetch('index', [
            'list' => $list = $model->getAgentOrderList($str,$page,$offset),
        ]);
    }
    /*
     * 获取当前登录代理所有下级代理id
     */
    private function getTree($data, $parent_username = '0', $is_first_time = true){
        static $arr = [];
        if ($is_first_time) {
            $arr = [];
        }
        foreach ($data as $key => $val) {
            if ($val['recommend_id'] == $parent_username) {
                $arr[]           = $val['agent_id'];
                $this->getTree($data, $val['agent_id'], false);
            }
        }
        return $arr;
    }
    /*
     * 品牌商审核
     */
    public function check(){
        $id=input('post.id');
        $status=input('post.status');
        $res=Db::name('agent_cashout')->where(['id'=>$id])->update(['pps_status'=>$status,'pps_update_time'=>time()]);
        if($res){
            return json(['code'=>'success','msg'=>'操作成功']);
        }else{
            return json(['code'=>'error','msg'=>'操作失败']);
        }
    }
}